package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsUserInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsUserInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.realize.PublicDealService;
import cc.rengu.igas.bsps.core.realize.impl.PublicDealServiceImpl;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.CashierMsgSettingRequest;
import cc.rengu.igas.bsps.facade.response.CashierMsgSettingResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/02
 */
public class CashierMsgSettingService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        CashierMsgSettingRequest cashierMsgSettingRequest = new CashierMsgSettingRequest();
        ConvertUtil.convertOutput(cashierMsgSettingRequest);
        return cashierMsgSettingRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        CashierMsgSettingRequest cashierMsgSettingRequest = (CashierMsgSettingRequest) request;
        CashierMsgSettingResponse cashierMsgSettingResponse = new CashierMsgSettingResponse();
        /*获取请求报文相关信息:法人行，用户标识，商户号，连锁商户号，收银员账号，关联收银员信息*/
        String instId = cashierMsgSettingRequest.getHeader().getInstId();
        String userId = cashierMsgSettingRequest.getUserId();
        String mchntNo = cashierMsgSettingRequest.getMchntNo();
        String cashierId = cashierMsgSettingRequest.getCashierId();
        String msgPushUsers = cashierMsgSettingRequest.getMsgPushUsers();

        rglog.info("请求报文信息:" + toJSONString(cashierMsgSettingRequest));
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) ||
                StringUtil.isEmptyOrNull(cashierId)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*根据商户号和登录账号查询用户信息*/
        rglog.info("查询收银员用户信息");
        BspsUserInfoMapper bspsUserInfoMapper = new BspsUserInfoMapperImpl();
        UserInfo userInfo = bspsUserInfoMapper.selectBspsUserInfoByUserId(instId, cashierId);
        if (null == userInfo) {
            rglog.error("查询收银员信息失败!法人行:<{}>，用户ID:<{}>", instId, cashierId);
            throw new BizException(RespCodeEnum.GET_USER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_INFO_ERROR.getRespDesc());
        }
        rglog.info("操作权限校验");
        /*判断用户是否有权限进行收银员的设置操作*/
        PublicDealService publicDealService = new PublicDealServiceImpl();
        boolean authFlag = publicDealService.checkUserOpAuth(instId, mchntNo, userId, userInfo.getUserId());
        if (!authFlag) {
            rglog.error("用户无权限设置收银员");
            throw new BizException(RespCodeEnum.USER_NO_AUTHORITY.getRespCode(), RespCodeEnum.USER_NO_AUTHORITY.getRespDesc());
        }

        /*根据唯一索引：instId+mchntNo+cashierId 更新商户绑定信息*/
        rglog.info("查询用户绑定商户信息");
        BspsMchntBindInfoMapper bspsMchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        MchntBindInfo mchntBindInfo = bspsMchntBindInfoMapper.selectBspsMchntBindInfoListByUserIdAndMchntNo(instId, cashierId, mchntNo);
        if (null == mchntBindInfo) {
            rglog.error("获取用户角色信息失败,mchntNo:<{}>,userId:<{}>", mchntNo, userId);
            throw new BizException(RespCodeEnum.GET_USER_ROLE_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_ROLE_INFO_ERROR.getRespDesc());
        }
        rglog.info("关联收银员信息设置：" + msgPushUsers);
        mchntBindInfo.setMsgPushUsers(msgPushUsers);
        /*更新最新操作相关信息*/
        mchntBindInfo.setLastOprId(userId);
        mchntBindInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = bspsMchntBindInfoMapper.updateBspsMchntBindInfoMsgUserByPrimaryKey(mchntBindInfo);
        if (0 != iReturnCode) {
            rglog.error("关联收银员信息推送失败,iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        /* 拼装响应结果 */
        BizResponse<CashierMsgSettingResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(cashierMsgSettingResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        CashierMsgSettingResponse cashierMsgSettingResponse = (CashierMsgSettingResponse) bizResponse.getResult();
        ConvertUtil.convertInput(cashierMsgSettingResponse);
    }
}
